package com.chenfan.mcn.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.common.utils.DateUtils;
import com.chenfan.mcn.annotations.ActionTracing;
import com.chenfan.mcn.config.UserVoContextHolder;
import com.chenfan.mcn.constant.McnConstant;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.enums.BusinessTypeEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.BusinessDataMoveLog;
import com.chenfan.mcn.model.dto.BusinessDataMoveDTO;
import com.chenfan.mcn.model.dto.BusinessDataMoveLogQueryDTO;
import com.chenfan.mcn.model.vo.BusinessTypeVO;
import com.chenfan.mcn.service.BusinessDataMoveService;
import com.chenfan.mcn.utils.BusinessAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 用户业务数据转移
 *
 * @author wulg
 * @date 2021-10-18
 **/
@Slf4j
@RestController
@RequestMapping("/businessDataMove")
public class BusinessDataMoveController {

    @Autowired
    private BusinessDataMoveService businessDataMoveService;

    @ActionTracing("用户业务数据转移")
    @PostMapping("/execute")
    public Res<Boolean> execute(@Validated @RequestBody BusinessDataMoveDTO dto) {
        log.info("收到用户业务数据转移请求，参数：{}", JSONObject.toJSONString(dto));
        BusinessAssert.isTrue(dto.getSourceUserId().longValue() != dto.getTargetUserId().longValue(),
                McnErrorCode.PARAM_ERROR, "源用户与目标用户须不同");
        BusinessDataMoveLog bdmLog = new BusinessDataMoveLog();
        Date now = new Date();
        bdmLog.setTaskCode(DateUtils.formatDate(now, McnConstant.BDM_CODE_PREFIX)+ ThreadLocalRandom.current().nextInt(100,999));
        bdmLog.setSourceUserId(dto.getSourceUserId());
        bdmLog.setTargetUserId(dto.getTargetUserId());
        bdmLog.setBusinessType(dto.getBusinessTypes());
        bdmLog.setStartTime(now);
        bdmLog.setStatus(NumberCst.ZERO);
        bdmLog.setOperatorId(UserVoContextHolder.getUserId());
        bdmLog.setOperatorName(UserVoContextHolder.getUserName());
        bdmLog.setTenantId(UserVoContextHolder.getTenantId());
        return Res.ok(businessDataMoveService.execute(bdmLog));
    }

    /**
     * 用户业务数据转移日志
     * @param businessDataMoveLogQueryDTO
     * @return
     */
    @PostMapping(value = "/log/query", produces = {"application/json;charset=UTF-8"})
    public Res<Page<BusinessDataMoveLog>> queryLogs(@RequestBody BusinessDataMoveLogQueryDTO businessDataMoveLogQueryDTO) {
        return Res.ok(businessDataMoveService.queryLogs(businessDataMoveLogQueryDTO));
    }

    /**
     * 获取所有业务类型枚举
     * @return
     */
    @GetMapping("/getBusinessTypes")
    public Res<List<BusinessTypeVO>> getBusinessTypes() {
        List<BusinessTypeVO> list = new ArrayList<>(BusinessTypeEnum.values().length);
        for (BusinessTypeEnum item : BusinessTypeEnum.values()) {
            list.add(new BusinessTypeVO(item.getType(), item.getName()));
        }
        return Res.ok(list);
    }
}
